#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/root.h>
#include<sys/link.h>
#include <sys/thread.h>
#define BUFFER 4096

int ID;

int Harry (void)
{
LinkCB_t *KetLink, *TonyLink;
char *FName = "Harry";
char answT[1000];
char answK[1000];
char *messg[] = {
	"Hello. Name?" 
	};
int error, i;


TonyLink = MakeLink(ID, 1234, &error);

if (TonyLink==NULL) {
	LogError(EC_ERROR, FName, "\make link Tony error = %d", error);
	exit(1);
}

KetLink = MakeLink(ID, 1235, &error);

if (KetLink==NULL) {
	LogError(EC_ERROR, FName, "\make link Ket error = %d", error);
	exit(1);
}
printf ("\nHarry->Tony  :%s", messg[0]);
SendLink (TonyLink, (byte*)messg[0], strlen(messg[0])+1);
printf ("\nHarry->Ket  :%s", messg[0]);
SendLink (KetLink, (byte*)messg[0], strlen(messg[0])+1);

RecvLink (TonyLink, (byte*)answT, 1000);
RecvLink (KetLink, (byte*)answK, 1000);

printf ("\nHarry<-Tony  : %s",answT);
printf ("\nHarry<-Ket  : %s",answK);

printf ("\nHarry->Tony  :%s",answK);
SendLink (TonyLink, (byte*)answK, strlen(answK)+1);
printf ("\nHarry->Ket  :%s", answT);
SendLink (KetLink, (byte*)answT, strlen(answT)+1);
exit (0);
}


int Tony (void)
{
LinkCB_t *HarryLink;
char *FName = "Tony";
char answ[1000];
char *messg[] = {"Hello. Is name Tony."};

int error, i;

HarryLink = GetLink(ID, 1234, &error);

if (HarryLink==NULL) {
	LogError(EC_ERROR, FName, "\nGet link Harry error = %d", error);
	exit(1);
}

	RecvLink (HarryLink, (byte*)answ, 1000);
	printf ("\nTony<-Harry  : %s \n Tony->Harry  :%s",answ, messg[0]);
	SendLink (HarryLink, (byte*)messg[0], strlen(messg[0])+1);
	RecvLink (HarryLink, (byte*)answ, 1000);
	printf ("\n Tony<-Harry  :%s",answ, messg[0]);
exit(0);
}

int Ket (void)
{
LinkCB_t *HarryLink;
char *FName = "Ket";
char answ[1000];
char *messg[] = {"Hello. Is name Ket."};
int error, i;

HarryLink = GetLink(ID, 1235, &error);

if (HarryLink==NULL) {
	LogError(EC_ERROR, FName, "\nGet link Harry error = %d", error);
	exit(1);
}

RecvLink (HarryLink, (byte*)answ, 1000);
printf ("\nKet<-Harry  : %s \n Ket->Harry  :%s",answ, messg[0]);
SendLink (HarryLink, (byte*)messg[0], strlen(messg[0])+1);
RecvLink (HarryLink, (byte*)answ, 1000);
printf ("\n Ket<-Harry  :%s",answ, messg[0]);
return 0;
}



int main(void)
{

Thread_t *HarryThread, *TonyThread, *KetThread;
char *FName = "main";
int error;
int result;

ID = GET_ROOT()->ProcRoot->MyProcID;

HarryThread = StartThread (Harry, BUFFER, &error, 0);
if (HarryThread==NULL){
	LogError (EC_ERROR, FName, "\nStarting Harry error = %d", error);
	exit(1);
}

TonyThread = StartThread (Tony, BUFFER, &error, 0);
if (TonyThread==NULL){
	LogError (EC_ERROR, FName, "Starting Tony error = %d", error);
	exit(1);
}

KetThread = StartThread (Ket, BUFFER, &error, 0);
if (KetThread==NULL){
	LogError (EC_ERROR, FName, "Starting Ket error = %d", error);
	exit(1);
}

WaitThread (TonyThread, &result);
WaitThread(KetThread,&result);
WaitThread (HarryThread, &result);
printf("finish");
exit(0);
}
